<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>添加/编辑招生简章</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="简章标题：" prop="name">
            <el-input v-model="editForm.title" auto-complete="off" placeholder="填写招生标题"></el-input>
          </el-form-item>
          <el-form-item label="拟招人数：" prop="name">
            <el-input v-model="editForm.number" auto-complete="off" placeholder="填写计划招生数"></el-input>
          </el-form-item>
          <el-form-item label="截止日期：">
            <el-date-picker type="date"
                            placeholder="选择日期"
                            v-model="editForm.end_time"
                            :picker-options="pickerOptions1"
                            value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="带看接待：" prop="num">
            <el-button @click.native="getMoreTeach(1)" size="small">选择带看接待人员</el-button>
            <el-button @click.native="userType=1,groupVisible=true" size="small">选择执行群组</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(1,line)"
                  class="mr-10"
                  v-for="(items,line) in ReceptionTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="审核人员：" prop="num">
            <el-button @click.native="getMoreTeach(2)" size="small">选择审核人员</el-button>
            <el-button @click.native="userType=2,groupVisible=true" size="small">选择执行群组</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(2,line)"
                  class="mr-10"
                  v-for="(items,line) in dynamicTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="招生简章：" prop="body">
            <div class="edit_container">
              <quill-editor v-model="editForm.body"
                            ref="myQuillEditor"
                            class="editer"
                            :options="editorOption"
                            placeholder="招生简章">
              </quill-editor>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定并保存</el-button>
      </div>
    </div>
    <!--    单选教职工审核人-->
    <MultipleTeacherModle :visible.sync="singleTeachVisible" @studentFn="studentFn"
                          :dynamicTagsArray="examine_user"></MultipleTeacherModle>
    <!--    选择群组-->
    <groupModule :visible.sync="groupVisible" @studentFn="studentFn"></groupModule>
  </div>
</template>

<script>
import {quillEditor} from 'vue-quill-editor' // 调用编辑器
import quillConfig from '@/assets/js/quill-config.js'
import {recuitAdd, recuitEdit, recuitDetail} from '@/api/admin/recuit'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

export default {
  props: ['visible', 'recruitId'], // commoent 学生点评 collection 学生采集
  components: {
    quillEditor,
  },
  data() {
    return {
      singleTeachVisible: false,
      groupVisible: false,
      dialogVisible: false,
      examine_user: {}, // 审核人
      dynamicTags: [],
      Reception_user: {},// 接待人员
      ReceptionTags: [],
      editForm: {
        title: '', // 名称
        users: '', // 接待人
        examine: '', // 审核人
        end_time: '',
        number: '', // 计划招生人数
        body: '', // 招生详情
      }, // 编辑界面数据
      // 限制时间选择
      editorOption: quillConfig, // 附文本框描述
      pickerOptions1:
          {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7
            }
          },
      userType: 0 // 1 招待 2 审核
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.recruitId) {
          this.getDetail();
        }
      } else {
        this.editForm = {
          title: '', // 名称
          users: '', // 接待人
          examine: '', // 审核人
          end_time: '',
          number: '', // 计划招生人数
          body: '', // 招生详情
        } // 编辑界面数据
        this.examine_user = {} // 审核人
        this.dynamicTags = []
        this.Reception_user = {}// 接待人员
        this.ReceptionTags = []
      }
    }
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill
    }
  },
  created() {
  },
  methods: {
    // 打开审核人
    getMoreTeach(option) {
      this.userType = option
      let a = {}
      let list_ = option === 1 ? this.ReceptionTags : this.dynamicTags
      for (let item of list_) {
        a[item.id] = item
      }
      this.examine_user = this.getJson(a)
      this.singleTeachVisible = true;
    },
    // 删除审核人
    handleCloseExamine(option, index) {
      if (option === 1) {
        this.ReceptionTags.splice(index, 1)
      } else {
        this.dynamicTags.splice(index, 1)
      }

    },
    // 审核人回执
    studentFn(option) {
      let list = []
      for (let i in option) {
        list.push(option[i])
      }
      if (this.userType === 1) {
        this.ReceptionTags = this.getJson(list)
      } else {
        this.dynamicTags = this.getJson(list)
      }
    },
    getDetail() {
      recuitDetail({id: this.recruitId}).then(res => {
        console.log('res:', res)
        // editForm: {
        //   title: '', // 名称
        //       users: '', // 接待人
        //       examine: '', // 审核人
        //       end_time: '',
        //       number: '', // 计划招生人数
        //       body: '', // 招生详情
        // }, // 编辑界面数据
        this.editForm.title = res.vo.title;
        this.editForm.end_time = res.vo.end_time;
        this.editForm.number = res.vo.number;
        this.editForm.body = res.vo.body;
        this.ReceptionTags = this.getJson(res.vo.users);
        this.dynamicTags = this.getJson(res.vo.examine);
        this.editForm.name = res.vo.name;
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getFile(option) {
      console.log('收到附件数据：', option)
      this.fileList = this.getJson(option)
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if (!dataOption.title) {
        this.$message.error('填写招生标题')
        return
      }
      if (!dataOption.end_time) {
        this.$message.error('请选择截止时间')
        return
      }
      if (!this.ReceptionTags.length) {
        this.$message.error('请选择招待人员')
        return
      }
      if (!this.dynamicTags.length) {
        this.$message.error('请选择审核人员')
        return
      }
      if (!dataOption.body) {
        this.$message.error('请填写招生内容')
        return
      }
      let a = []
      for (let item of this.dynamicTags) {
        a.push(item.id)
      }
      let b = []
      for (let item of this.ReceptionTags) {
        b.push(item.id)
      }
      dataOption.users = b.join(',')
      dataOption.examine = a.join(',')
      if (this.recruitId) {
        dataOption.id = this.recruitId
        recuitEdit(dataOption).then(res => {
          if (!res.code) {
            this.$message.success('编辑成功！')
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        recuitAdd(dataOption).then(res => {
          if (!res.code) {
            this.$message.success('编辑成功！')
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 80%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


